package domain.model.project.support.projectid;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import domain.model.project.ProjectId;
import util.jackson.serializer.JacksonWriter;

import java.io.IOException;

/**
 * @author Gregory Boissinot
 */
public class ProjectIdSerializer extends JsonSerializer<ProjectId> {

    private final JacksonWriter jacksonWriter;

    public ProjectIdSerializer() {
        this.jacksonWriter = new JacksonWriter();
    }

    @Override
    public void serialize(ProjectId projectId,
                          JsonGenerator jsonGenerator,
                          SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jacksonWriter.writeMandatoryFieldValue(jsonGenerator, "org", projectId.getOrganization());
        jacksonWriter.writeMandatoryFieldValue(jsonGenerator, "name", projectId.getName());
        jsonGenerator.writeEndObject();
    }
}
